﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace Gmantis.Controls
{
    internal static class CompositeTextUtils
    {
        // Methods
        public static string ConvertSymbolListToString(IList<string> symbols)
        {
            StringBuilder builder = new StringBuilder(symbols.Count);
            foreach (string str in symbols)
            {
                builder.Append(str);
            }
            return builder.ToString();
        }

        public static IList<string> ConvertToSymbolList(string str)
        {
            bool flag = string.IsNullOrEmpty(str);
            List<string> list = new List<string>(flag ? 0 : str.Length);
            if (!flag)
            {
                TextElementEnumerator textElementEnumerator = StringInfo.GetTextElementEnumerator(str);
                while (textElementEnumerator.MoveNext())
                {
                    list.Add(textElementEnumerator.GetTextElement());
                }
            }
            return list;
        }

        public static TextChangeInfo GetTextChange(IList<string> oldTextArr, IList<string> newTextArr, int cursorPosition)
        {
            TextChangeInfo info = new TextChangeInfo();
            int count = oldTextArr.Count;
            int num2 = newTextArr.Count;
            int num3 = Math.Min(count, num2);
            int num4 = num3;
            for (int i = 0; i < num3; i++)
            {
                if (oldTextArr[i] != newTextArr[i])
                {
                    num4 = i;
                    break;
                }
            }
            if ((num4 != count) || (count != num2))
            {
                int num6 = 0;
                if (num4 < count)
                {
                    int num7 = count - 1;
                    for (int j = num2 - 1; (num7 >= 0) && (j >= 0); j--)
                    {
                        if (oldTextArr[num7] != newTextArr[j])
                        {
                            break;
                        }
                        num6++;
                        num7--;
                    }
                }
                bool flag = false;
                if (count <= num2)
                {
                    if ((num4 + num6) > count)
                    {
                        flag = true;
                        num6 = count - num4;
                    }
                    int num9 = num2 - count;
                    int num10 = (count - num4) - num6;
                    info.Offset = num4;
                    info.AddedLength = num9 + num10;
                    info.RemovedLength = num10;
                }
                else
                {
                    if ((num4 + num6) > num2)
                    {
                        flag = true;
                        num6 = num2 - num4;
                    }
                    int num11 = count - num2;
                    int num12 = (count - num4) - num6;
                    info.Offset = num4;
                    info.AddedLength = num12 - num11;
                    info.RemovedLength = num12;
                }
                if (flag)
                {
                    int num13 = cursorPosition - info.AddedLength;
                    if (num13 >= 0)
                    {
                        info.Offset = num13;
                    }
                }
            }
            return info;
        }
    }

    internal class TextChangeInfo
    {
        // Properties
        public int AddedLength { get; set; }

        public int Offset { get; set; }

        public int RemovedLength { get; set; }
    }
}
